package com.test.card.controller;


import com.test.card.bean.UserInfo;
import com.test.card.contants.UserContants;
import com.test.card.domain.ApiReponse;
import com.test.card.enums.ApiResponseEnum;
import com.test.card.exception.ParameterException;
import com.test.card.service.IUserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/card")
public class UserCardController {
    @Autowired
    private IUserInfoService userInfoService;

    /**
     * 卡激活
     * @param userinfo
     * @param bindingResult
     * @return
     */
    @PostMapping("/activate/{state}")
    public ApiReponse<Integer> cardActivate(@PathVariable Integer state, @RequestBody @Validated UserInfo userinfo, BindingResult bindingResult ){
        if (bindingResult.hasErrors()) {
            throw new ParameterException(bindingResult.getFieldError().getDefaultMessage());
        }

       Integer count = userInfoService.acitvateCard(userinfo,state);
        return new ApiReponse<>(ApiResponseEnum.SUCCESS,count);
    }

    /**
     * 批量激活
     * @param userinfo
     * @param bindingResult
     * @return
     */
    @PostMapping("/batchActivate")
    public ApiReponse<Integer> carBatchActivate(@RequestBody List<UserInfo> userinfo, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            throw new ParameterException(bindingResult.getFieldError().getDefaultMessage());
        }

        Integer count = 0;
        for (UserInfo us: userinfo) {
            count += userInfoService.acitvateCard(us, UserContants.ACTIVATE);
        }

        return new ApiReponse<>(ApiResponseEnum.SUCCESS,count);
    }
}
